package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class LeetCodeTest0145 {

    public static void main(String[] args) {

    }

    public List<Integer> postorderTraversal(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        Stack<TreeNode> treeNodeStack = new Stack<>();
        Stack<Integer> resultStack = new Stack<>();
        treeNodeStack.push(root);
        while (!treeNodeStack.isEmpty()) {
            TreeNode node = treeNodeStack.pop();
            resultStack.push(node.val);
            if (node.left != null) {
                treeNodeStack.push(node.left);
            }
            if (node.right != null) {
                treeNodeStack.push(node.right);
            }
        }
        ArrayList<Integer> result = new ArrayList<>();
        while (!resultStack.isEmpty()) {
            result.add(resultStack.pop());
        }
        return result;
    }
}
